package jsu.blogger.mapper;

import jsu.blogger.pojo.Post;
import jsu.blogger.pojo.Users;
import org.apache.ibatis.annotations.*;
import org.springframework.web.bind.annotation.RequestParam;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface LiveMapper {
    @Insert("insert into blog_system_database.posts (user_id,title,content, post_type,created_at) value (#{userId},1,#{content},1,#{createdAt})")
    @Options(useGeneratedKeys=true, keyProperty="postId", keyColumn="post_id")
    public int insertOne(Post post);
    @Select("select post_id as 'postId'" +
            ",user_id as 'userId' " +
            ",content" +
            ",created_at as 'createdAt'" +
            ",likes_count as 'likesCount'" +
            ",comments_count as 'commentsCount'" +
            " from blog_system_database.posts where post_id=#{postId}")
    public Post selectLiveBuPostId(@Param("postId")Long postId);
    @Select("select followed_id from blog_system_database.user_focus where follower_id=#{uid}")
    public List<Long> selectFollowedUsersByUid(@RequestParam("uid") Long uid);
    @Select("select user_focus.follower_id from blog_system_database.user_focus where followed_id=#{uid}")
    public List<Long> selectFollowersByUid(@Param("uid") Long uid);
    @Select("select post_id as 'postId'," +
            "user_id as 'userId'," +
            "content," +
            "created_at as 'createdAt'," +
            "likes_count as'likesCount'" +
            "from blog_system_database.posts where posts.post_type=1 limit #{offset},#{limit}")
    public List<Post> selectLiveAllLimit(@Param("offset") int offset, @Param("limit") int limit);
    @Select("select post_id as 'postId'," +
            "user_id as 'userId'," +
            "content," +
            "created_at as 'createdAt'," +
            "likes_count as'likesCount'" +
            "from blog_system_database.posts where posts.post_type=1 and user_id=#{uid} limit #{offset},#{limit}")
    public List<Post> selectLiveByUid(@Param("uid")Long uid,@Param("offset") int offset, @Param("limit") int limit);
    @Select("select post_id as 'postId'," +
            "user_id as 'userId'," +
            "content," +
            "created_at as 'createdAt'," +
            "likes_count as'likesCount'" +
            " from blog_system_database.posts where posts.post_type=1")
    List<Post> selectLiveAll();
    @Select("select created_at from blog_system_database.user_focus where follower_id=#{uid1}  and followed_id=#{uid2}")
    public Timestamp isUser1FollowUser2(@Param("uid1") Long uid1, @Param("uid2")Long uid2);
    @Update("insert into user_focus (follower_id, followed_id, created_at) values(#{uid1},#{uid2},#{createdAt})")
    public int insertUserFollow(@Param("uid1") Long uid1, @Param("uid2") Long uid2,@Param("createdAt")Timestamp timestamp);
    @Delete("delete  from user_focus where follower_id=#{uid1} and followed_id=#{uid2}")
    public int deleteUserFollow(@Param("uid1") Long uid1, @Param("uid2") Long uid2);
    @Delete("delete  from posts where user_id=#{uid}")
    public int deleteLiveByUid(@Param("uid") Long uid);
    @Delete("delete  from posts where posts.post_id=#{postId}")
    public int deleteLiveByPostId(@Param("postId") Long postId);
    @Delete("delete  from post_tags where post_id=#{postId}")
    public int deleteLiveTagsByPostIdToDeletePost(@Param("postId") Long postId);
    @Update("update posts set likes_count=likes_count+1 where post_id=#{postId} ")
    public int updatePostLikeAddOneUpdate(
            @Param("postId")Long postId
    );
    @Insert("insert into blog_system_database.post_likes (user_id, post_id, is_like, created_at) " +
            "values(#{uid},#{postId},#{isLike},#{createdAt})")
    public int updatePostLikeAddOneInsert(
            @Param("postId")Long postId,
            @Param("uid")Long uid,
            @Param("isLike")int isLike,
            @Param("createdAt")Timestamp timestamp
    );
    @Select("select posts.likes_count from posts where post_id=#{postId}")
    public int selectLikeCountByPostId(@Param("postId") Long postId);
    @Select("select is_like from blog_system_database.post_likes where post_id=#{postId} and user_id=#{uid}")
    public Integer selectIsLikeByPostIdAndUid(@Param("postId") Long postId,@Param("uid")Long uid);
    @Delete("delete from blog_system_database.post_likes where post_id=#{postId} and user_id=#{uid}")
    public int deletePostLikeDelete(@Param("postId") Long postId,@Param("uid")Long uid);
    @Update("update posts set likes_count=likes_count-1 where likes_count>0 and post_id=#{postId} ")
    public int deletePostLikeUpdate(@Param("postId") Long postId,@Param("uid")Long uid);

    @Select("select content from  posts where post_id=#{postId}")
    public String selectPostContentByPostId(@Param("postId") Long postId);
    @Select("select user_id from  posts where post_id=#{postId}")
    public Long selectUserIdByPostId(@Param("postId") Long postId);
    @Select("SELECT post_id FROM posts " +
            "WHERE post_type=0 and user_id IN (" +
            "SELECT followed_id FROM blog_system_database.user_focus " +
            "WHERE follower_id = #{userId}) " +
            "LIMIT #{begin}, #{count}")
    List<Long> selectPostIdByUserId(@Param("userId") Long userId, @Param("begin") int begin, @Param("count") int count);

}